﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using AutoBuyWinform.entity;
using System.Threading;
using System.Collections;

namespace AutoBuyWinform
{
    public partial class LastTimeForm : Form
    {
        private Operations operations;
        private CookieContainer cc;
        private CookieCollection ccReturned;

        private HashSet<String> urls;
        private HashSet<String> reBuyUrls;
        private Thread t;

        int logLength = 1024;

        public LastTimeForm(HashSet<String> urls, CookieContainer cc)
        {
            this.operations = new Operations();
            this.cc = cc;
            this.urls = urls;
            this.reBuyUrls = new HashSet<string>();
            //this.ccReturned = ccReturned;
            InitializeComponent();
        }

        public void startLastTime()
        {
            wait();
            
        }

        private void startBuy(HashSet<String> urls)
        {

            foreach (String url in urls)
            {
                string page = Util.GetPage(url, Util.GetCookieContainerCopy(cc), out ccReturned);
                if (ccReturned != null)
                    cc.Add(ccReturned);
                Product product = operations.GetProduct(page);
                
                if (product == null)
                {
                    reBuyUrls.Add(url);
                    //MessageBox.Show("没有找到商品信息");
                    continue;
                }

                reBuyUrls.Remove(url);

                string prodMs = product.prodTypeDesc + " " + product.ph + " " + product.hd + " " + product.price + "元";

                product.url = url;
                operations.AddToCart(product, Util.GetCookieContainerCopy(cc));

                this.Invoke(new Action(() =>
                {
                    setMsg("开始抢拍:" + prodMs + url, true);
                }));

                
                Thread.Sleep(Form1.buyWaitTime);
            }
            
        }

        private void reBuy()
        {

            int i = 0;
            while(i<5 && reBuyUrls.Count>0)
            {
                startBuy(reBuyUrls);
                i++;
                Application.DoEvents();
            }

            foreach (string url in reBuyUrls)
            {
                this.Invoke(new Action(() =>
                {
                    setMsg("无法提取商品信息:" + url, true);
                }));

            }

        }

        private void wait()
        {
            t = new Thread(() =>
            {
                DateTime startTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, Form1.startBuy_h, Form1.startBuy_m, Form1.startbuy_s);
                while (true)
                {
                    TimeSpan wt = startTime.TimeOfDay - DateTime.Now.TimeOfDay;
                    //labelTime.Text = new DateTime(startTime.TimeOfDay.Ticks - DateTime.Now.TimeOfDay.Ticks).TimeOfDay.ToString();
                    this.Invoke(new Action(()=>
                    {
                        this.labelTime.Text = ""+wt.ToString().Substring(0,8);
                    }));

                    if (wt.Ticks <= 0)
                    {
                        break;
                    }
                    if (wt.Ticks > 1000 * 10000)
                    {
                        Thread.Sleep(1000);
                    }
                    else
                    {
                        Thread.Sleep(wt);
                    }
                }

                this.Invoke(new Action(() =>
                {
                    this.startBuy(this.urls);
                    this.reBuy();
                }));

                Application.Run();
            });
            t.SetApartmentState(ApartmentState.STA);
            t.IsBackground = true;
            t.Start();

            
        }

        private void LastTime_FormClosing(object sender, FormClosingEventArgs e)
        {
            t.Abort();
        }

        public void setMsg(String msg, bool noLengthLimit)
        {
            int length = this.logLength;
            if (noLengthLimit)
            {
                length = int.MaxValue;
            }

            setMsg(msg);
        }

        public void setMsg(String msg)
        {
            logBox.Text = DateTime.Now + ":" + msg + "\r\n" + logBox.Text.Substring(0, logBox.Text.Length > logLength ? logLength : logBox.Text.Length);
            Refresh();
        }

    }
}
